//
//  ZHLoginViewController.m
//  Doctor
//
//  Created by liuzhishan on 15/12/14.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHLoginViewController.h"
#import "HomeViewController.h"
#import "ZHLoginView.h"
#import "ZHHomeNaviViewController.h"
#import "ZHUseMesage.h"

@interface ZHLoginViewController ()<LoginDele,pushRootDele>
@property (nonatomic,retain) ZHLoginView *loginView;

@property (nonatomic,retain) ZHUseMesage *userMessageModel;

@end

@implementation ZHLoginViewController


-(void)getNewRootController
{
    
    ZHLoginViewController *login = [[ZHLoginViewController alloc]init];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = nil;
    [UIApplication sharedApplication].keyWindow.rootViewController = login;
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.naviTitleLable.text = @"登陆";
    
    [self addMyChildViews];
    
  
    
    
    //添加键盘通知
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeYShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyHidden:) name:UIKeyboardWillHideNotification object:nil];
   
    
}


-(void)KeYShow:(NSNotification *)noti
{
    //nslog(@"%@",noti);
    
    
    [UIView animateWithDuration:1 animations:^{
        
         self.loginView.y  = 80 * HEIPro;
        
    }];
    
}

-(void)KeyHidden:(NSNotification *)noti
{
    [UIView animateWithDuration:1 animations:^{
        
    self.loginView.y  = 179 * HEIPro;
        
    }];
    
}


-(void)addMyChildViews
{
    
    
    self.loginView = [[ZHLoginView alloc]init];
    [self.loginView setMyFramewithX:303 andY:179 andWidth:435 andHeight:365];
    [self.view addSubview:self.loginView];
    self.loginView.dele  = self;

}




-(void)loginBtnisClick:(NSString *)userName andPsw:(NSString *)userPsw
{
    
    self.loginView.btnLogin.userInteractionEnabled = NO;
 
    NSString *loginUrl = [NSString stringWithFormat:@"%@",LOGINURL];

    
    //userName = [userName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSLog(@"%@",userName);
    NSLog(@"%@",userPsw);
    
    NSDictionary *parameters = @{@"username":userName,@"password":userPsw};

 
  [AFNData postBodyAFNDataWithUrl:loginUrl andBodyDic:parameters success:^(id responseObject) {

      //nslog(@"999接口0上传====%@",parameters);
      //nslog(@"999返回====%@",responseObject);
 
 
      NSLog(@"%@",responseObject);
      
      NSDictionary *dic = responseObject;
      
      NSNumber *num = dic[@"code"];
      
      NSInteger numInt = [num integerValue];
      
      dic = [dic  objectForKey:@"payload"];
      
      
      self.userMessageModel = [[ZHUseMesage alloc]init];
      
      [self.userMessageModel setValuesForKeysWithDictionary:dic];
      
      [[NSUserDefaults standardUserDefaults] setObject:self.userMessageModel.userIdentity forKey:@"userMessage"];
      
      
      [self alertUSerMessage:numInt];
      
      self.loginView.btnLogin.userInteractionEnabled = YES;

      
      
  } failure:^(NSError *error) {
      
       self.loginView.btnLogin.userInteractionEnabled = YES;
      
  }];
  
    
}


-(void)alertUSerMessage:(NSInteger)index
{
    
    
    if ( index == 0) {
        
        
    }
    
    else if (index == 1)
    {
        [self.loginView alertUP:1.0 withMessage:@"亲～～用户被锁定"];
    }
    
    
    else if (index == 2)
    {
        [self.loginView alertUP:1.0 withMessage:@"亲～～用户名错误"];
    }
    
    else if (index == 3)
    {
        [self.loginView alertDown:1.0];
    }
    
    
    else if (index == 5)
    {
        [self.loginView saveTheUserMessageToLocal];
        
        HomeViewController *home = [[HomeViewController alloc]init];
        home.dele = self;
        
        ZHHomeNaviViewController *navi = [[ZHHomeNaviViewController alloc]initWithRootViewController:home];
        
        
        [self presentViewController:navi animated:YES completion:^{
            
            
        }];

    }

    
    else
    {
        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
